import React from 'react';
import { Button, Input, Form, message } from 'antd';
import request from '@/utils/request';
import CateComp from '@/components/CategoryComponent';

const apiURL = '/api/services/app/Warehse/';
class WarehseEntry extends React.Component<any, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      loading: false,
      dataSource: [],
    };
  }
  formRef: any = React.createRef();
  componentDidMount() {
    this.fetchDatas();
  }
  onFinish = (values: any) => {
    var model = values.model;
    model.id = this.props.id;
    this.setState({ loading: true });
    var act = 'Modify';
    if (model.id == 0 || !model.id) {
      act = 'New';
    }
    model.zip = '0';
    model.city = '0';
    model.state = '0';
    model.faxNum = '0';
    model.address = '0';
    model.country = '0';
    model.phoneNum = '0';
    request(apiURL + act, {
      method: 'POST',
      data: model,
    }).then((response) => {
      if (response) {
        if (response.success) {
          if (response.result.success) {
            message.success('保存成功！');
            this.props.fetchData();
          } else {
            message.error(response.result.message);
          }
        }
        this.setState({ loading: false });
      }
    });
  };
  fetchDatas = () => {
    const { id } = this.props;
    if (id > 0) {
      request(apiURL + 'Get', {
        method: 'GET',
        params: { Id: id },
      }).then((response) => {
        var model = response?.result;
        this.formRef.current.setFieldsValue({ model });
      });
    }
  };
  render() {
    const layout = {
      labelCol: { span: 5 },
      wrapperCol: { span: 19 },
    };
    return (
      <Form {...layout} ref={this.formRef} onFinish={this.onFinish}>
        <Form.Item name={['model', 'warehouseCode']} label="仓库代码" rules={[{ required: true }]}>
          <Input />
        </Form.Item>
        <Form.Item name={['model', 'description']} label="描述" rules={[{ required: true }]}>
          <Input />
        </Form.Item>
        <CateComp
          layout="仓库类型"
          width={200}
          name={['model', 'warehouseType']}
          label="仓库类型"
          required
        />
        <Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 5 }}>
          <Button type="primary" htmlType="submit">
            保存
          </Button>
        </Form.Item>
      </Form>
    );
  }
}
export default WarehseEntry;
